package com.ZeroCarbon.domain.dto;

import cn.hutool.core.bean.BeanUtil;
import com.ZeroCarbon.domain.po.Identity;
import com.ZeroCarbon.exception.FileHandingException;
import com.ZeroCarbon.utils.FileHandingUtils;
import com.ZeroCarbon.utils.JsonUtils;
import com.ZeroCarbon.utils.UserHolder;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

import static com.ZeroCarbon.prefix.FilePrefix.IDENTITY_IMAGES;
import static com.ZeroCarbon.prefix.MySqlKey.SHOP_PREFIX;

/**
 * 身份信息类
 *
 * @since 2024-11-8
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class IdentityReceiverDTO {
    /**
     * 身份id
     */
    private String id;

    /**
     * 用户id
     */
    private String userId;

    /**
     * 名称
     */
    private String name;

    /**
     * 描述
     */
    private String description;

    /**
     * 标签
     */
    private List<String> tags;

    /**
     * 图片
     */
    private MultipartFile image;

    /**
     * IdentityReceiverDTO转PO
     */
    public Identity toIdentity() throws FileHandingException {
        Identity identity = BeanUtil.copyProperties(this, Identity.class);
        //获取文件对象
        MultipartFile image = this.image;
        //上传到文件夹
        String file = FileHandingUtils.uploadFile(IDENTITY_IMAGES, image);
        //设置identity信息
        identity.setImage(JsonUtils.ObjectToJson(file))
                .setTagsJson(JsonUtils.ObjectToJson(tags))
                .setUserId(UserHolder.getUser());
        //生成id
        if (identity.getId() == null) {
            String id = SHOP_PREFIX + UUID.randomUUID();
            identity.setId(id);
        }
        return identity;
    }
}
